package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsComment;
import com.uyayo.cms.entity.CmsCommentLike;
import com.uyayo.cms.entity.CmsReply;
import com.uyayo.cms.entity.CmsReplyLike;
import com.uyayo.cms.mapper.CmsReplyLikeMapper;
import com.uyayo.cms.mapper.CmsReplyMapper;
import com.uyayo.cms.model.*;
import com.uyayo.cms.service.CmsCommentService;
import com.uyayo.cms.service.CmsReplyService;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsReplyService")
public class CmsReplyServiceImpl implements CmsReplyService {


	@Autowired
	private CmsReplyMapper cmsReplyMapper;

	@Autowired
	private CmsCommentService cmsCommentService;

	@Autowired
	private CmsReplyLikeMapper cmsReplyLikeMapper;

	@Override
	public CmsReply getReplyById(Long id){
		return cmsReplyMapper.selectByPrimaryKey(id);
	}
	

	@Override
	public List<CmsReply> selectReply(CmsReplyCondition cmsReplyCondition) {
		Example example = new Example(CmsReply.class);
		Criteria criteria = example.createCriteria();
		if(Objects.nonNull(cmsReplyCondition.getStatus())){
			criteria.andEqualTo("status",cmsReplyCondition.getStatus());
		}
		if(Objects.nonNull(cmsReplyCondition.getAuditStatus())){
			criteria.andEqualTo("auditStatus",cmsReplyCondition.getAuditStatus());
		}
		if(Objects.nonNull(cmsReplyCondition.getArticleId())){
			criteria.andEqualTo("articleId",cmsReplyCondition.getArticleId());
		}
		if(Objects.nonNull(cmsReplyCondition.getCommentId())){
			criteria.andEqualTo("commentId",cmsReplyCondition.getCommentId());
		}
		if(Objects.nonNull(cmsReplyCondition.getReplyToMemberId())){
			criteria.andEqualTo("replyToMemberId",cmsReplyCondition.getReplyToMemberId());
		}
		return cmsReplyMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<CmsReply> selectReplyByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsReplyCondition cmsReplyCondition) {

		List<CmsReply> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(CmsReply.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsReplyCondition.getContent())){
			criteria.andLike("content","%"+cmsReplyCondition.getContent()+"%");
		}
		if(Objects.nonNull(cmsReplyCondition.getArticleId())){
			criteria.andEqualTo("articleId",cmsReplyCondition.getArticleId());
		}
		if(Objects.nonNull(cmsReplyCondition.getCommentId())){
			criteria.andEqualTo("commentId",cmsReplyCondition.getCommentId());
		}
		if(Objects.nonNull(cmsReplyCondition.getReplyToMemberId())){
			criteria.andEqualTo("replyToMemberId",cmsReplyCondition.getReplyToMemberId());
		}
		if(cmsReplyCondition.getBeginCreateTime()!=null || cmsReplyCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsReplyCondition.getBeginCreateTime(), cmsReplyCondition.getEndCreateTime());
		}
		userList = cmsReplyMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsReply> pageInfo = new PageInfo<>(userList);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}



	@Override
	public PageBean<CmsReplyBO> selectReplyListByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsReplyCondition cmsReplyCondition) {

		List<CmsReplyBO> userList;
		PageHelper.startPage(pageCurrent, pageSize);
		userList = cmsReplyMapper.selectCmsReplyList(cmsReplyCondition);

		//取记录总条数
		PageInfo<CmsReplyBO> pageInfo = new PageInfo<>(userList);

		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}

	@Override
	public int createCmsReply(CmsReply cmsReply){
		CmsCommentCountBO cmsCommentCountBO = new CmsCommentCountBO();
		cmsCommentCountBO.setId(cmsReply.getCommentId());
		cmsCommentCountBO.setReplyCount(1);
		cmsCommentService.updateCommentCount(cmsCommentCountBO);
		return cmsReplyMapper.insertSelective(cmsReply);

	}

	private int selectCountByMemberId(Long memberId) {
		CmsReply cmsReply = new CmsReply();
		cmsReply.setReplyFromMemberId(memberId);
		return cmsReplyMapper.selectCount(cmsReply);
	}

	@Override
	public CmsReply selectOne(CmsReply cmsReply) {
		return cmsReplyMapper.selectOne(cmsReply);
	}

	@Override
	public int updateReply(CmsReply cmsReply){
		return cmsReplyMapper.updateByPrimaryKeySelective(cmsReply);
	}

	@Override
	public int deleteReply(Long id) {
		return cmsReplyMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateReplyCount(CmsReplyCountBO cmsReplyCountBO) {
		CmsReply cmsReply = new CmsReply();
		cmsReply.setId(cmsReplyCountBO.getId());
		cmsReply = this.selectOne(cmsReply);
		if(Objects.nonNull(cmsReplyCountBO.getReplyCount())){
			cmsReply.setReplyCount(cmsReply.getReplyCount()+cmsReplyCountBO.getReplyCount());
		}

		if(Objects.nonNull(cmsReplyCountBO.getLikeCount())){
			cmsReply.setLikeCount(cmsReply.getLikeCount()+cmsReplyCountBO.getLikeCount());
		}
		if(Objects.nonNull(cmsReplyCountBO.getDislikeCount())){
			cmsReply.setDislikeCount(cmsReply.getDislikeCount()+cmsReplyCountBO.getDislikeCount());
		}

		return cmsReplyMapper.updateByPrimaryKeySelective(cmsReply);
	}

	@Override
	public int like(CmsReplyLike cmsReplyLike) {
		CmsReplyLike cmsLikeCondition = new CmsReplyLike();
		cmsLikeCondition.setReplyId(cmsReplyLike.getReplyId());
		cmsLikeCondition.setLikeType(cmsReplyLike.getLikeType());
		cmsLikeCondition.setMemberId(cmsReplyLike.getMemberId());
		List<CmsReplyLike> cmsReplyLikeList = cmsReplyLikeMapper.select(cmsLikeCondition);
		if(cmsReplyLikeList != null && cmsReplyLikeList.size()==1){//查询出数据才进行下一步
			CmsReplyLike cmsReplyLikeDB = cmsReplyLikeList.get(0);
			if(cmsReplyLikeList.get(0).getStatus().equals(cmsReplyLike.getStatus())){ //状态和数据库里的相同则无需处理
				return 0;
			}

			CmsReplyCountBO cmsReplyCountBO = new CmsReplyCountBO();
			cmsReplyCountBO.setId(cmsReplyLike.getReplyId());
			if(cmsReplyLike.getLikeType()==1 && cmsReplyLike.getStatus()==0){ //取消赞
				cmsReplyLikeDB.setStatus((short) 0);
				cmsReplyCountBO.setLikeCount(-1);
				this.updateReplyCount(cmsReplyCountBO);
			}else if(cmsReplyLike.getLikeType()==1 && cmsReplyLike.getStatus()==1){	//赞
				cmsReplyLikeDB.setStatus((short) 1);
				cancelDislike(cmsReplyLike.getReplyId(),cmsReplyLike.getMemberId());
			}else if(cmsReplyLike.getLikeType()==2 && cmsReplyLike.getStatus()==0){	//取消踩
				cmsReplyLikeDB.setStatus((short) 0);
				cmsReplyCountBO.setDislikeCount(-1);
				this.updateReplyCount(cmsReplyCountBO);
			}else {	//踩(cmsReplyLike.getLikeType()==2 && cmsReplyLike.getStatus()==1)
				cmsReplyLikeDB.setStatus((short) 1);
				cancelLike(cmsReplyLike.getReplyId(),cmsReplyLike.getMemberId());
			}
			return  this.cmsReplyLikeMapper.updateByPrimaryKeySelective(cmsReplyLikeDB);
		}else{
			if(cmsReplyLike.getLikeType()==2){
				cancelLike(cmsReplyLike.getReplyId(),cmsReplyLike.getMemberId());
			}else {
				cancelDislike(cmsReplyLike.getReplyId(),cmsReplyLike.getMemberId());
			}
			return cmsReplyLikeMapper.insertSelective(cmsReplyLike);
		}
	}

	/**
	 * 取消赞
	 * @param replyId 评论ID
	 * @param memberId 会员ID
	 */
	private void cancelLike(Long replyId,Long memberId){
		CmsReplyLike cmsDisLikeOppositeCondition = new CmsReplyLike();
		cmsDisLikeOppositeCondition.setReplyId(replyId);
		cmsDisLikeOppositeCondition.setLikeType((short) 1);
		cmsDisLikeOppositeCondition.setMemberId(memberId);
		CmsReplyLike cmsReplyDisLikeOppositeDB = cmsReplyLikeMapper.selectOne(cmsDisLikeOppositeCondition);
		if(cmsReplyDisLikeOppositeDB!=null  && cmsReplyDisLikeOppositeDB.getStatus()==1){
			cmsReplyDisLikeOppositeDB.setStatus((short) 0);
			this.cmsReplyLikeMapper.updateByPrimaryKeySelective(cmsReplyDisLikeOppositeDB);

			//评论的赞数量-1
			CmsReplyCountBO cmsReplyDisLikeCountOppositeBO = new CmsReplyCountBO();
			cmsReplyDisLikeCountOppositeBO.setId(replyId);
			cmsReplyDisLikeCountOppositeBO.setLikeCount(-1);
			this.updateReplyCount(cmsReplyDisLikeCountOppositeBO);
		}

		//评论的踩数量+1
		CmsReplyCountBO cmsReplyCountBO = new CmsReplyCountBO();
		cmsReplyCountBO.setId(replyId);
		cmsReplyCountBO.setDislikeCount(1);
		this.updateReplyCount(cmsReplyCountBO);
	}

	/**
	 * 取消踩
	 * @param replyId 评论ID
	 * @param memberId 会员ID
	 */
	private void cancelDislike(Long replyId,Long memberId){
		//取消踩
		CmsReplyLike cmsLikeOppositeCondition = new CmsReplyLike();
		cmsLikeOppositeCondition.setReplyId(replyId);
		cmsLikeOppositeCondition.setLikeType((short) 2);
		cmsLikeOppositeCondition.setMemberId(memberId);
		CmsReplyLike cmsReplyLikeOppositeDB = cmsReplyLikeMapper.selectOne(cmsLikeOppositeCondition);
		if(cmsReplyLikeOppositeDB!=null  && cmsReplyLikeOppositeDB.getStatus()==1){
			cmsReplyLikeOppositeDB.setStatus((short) 0);
			this.cmsReplyLikeMapper.updateByPrimaryKeySelective(cmsReplyLikeOppositeDB);

			//评论的踩数量-1
			CmsReplyCountBO cmsReplyLikeCountOppositeBO = new CmsReplyCountBO();
			cmsReplyLikeCountOppositeBO.setId(replyId);
			cmsReplyLikeCountOppositeBO.setDislikeCount(-1);
			this.updateReplyCount(cmsReplyLikeCountOppositeBO);
		}

		//评论的赞数量+1
		CmsReplyCountBO cmsReplyCountBO = new CmsReplyCountBO();
		cmsReplyCountBO.setId(replyId);
		cmsReplyCountBO.setLikeCount(1);
		this.updateReplyCount(cmsReplyCountBO);
	}
}
